//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Controllers;

using $prjsn$.Common;
using $prjsn$.Common.Business;
using $prjsn$.Common.Models;
using $prjsn$.Entity;

namespace $safeprojectname$.Controllers
{
    /// <summary>
    ///     Overrides base controller action 'ComboBox', to return AllVisible values
    /// </summary>
    public abstract class FieldObjectController<TField, TModel, TFacade>
        : EntityController<TField, FieldFilter, TModel, TFacade>
        where TField : FieldObject, new()
        where TModel : class, IFieldObjectModel<TField>, IEditModel<TField, int, FieldFilter>
        where TFacade : class, IFieldObjectFacade<TField>
    {
        /// <summary>List View</summary>
        protected override IEnumerable<IItemToDisplay> LoadItemsToDisplay(FieldFilter filter)
        {
            return Facade.Find(filter)
                .Select(i => new ItemToDisplay
                {
                    ID = i.IDToDisplay,
                    Description = i.ToDisplay(),
                    Items = new List<ICellDescription>
                    {
                        i.Cell(e => e.PropertyName),
                        i.Cell(e => e.ColumnName),
                        i.Cell(e => e.DisplayName),
                        i.Cell(e => e.DataType),
                        i.Cell(e => e.Length),
                        i.Cell(e => e.MinValue),
                        i.Cell(e => e.MaxValue),
                        i.Cell(e => e.IsStatic),
                        i.Cell(e => e.IsRequired),
                        i.Cell(e => e.IsUnique),
                        i.Cell(e => e.TableName),
                        i.Cell(e => e.ID, new CellDescription {Align = Align.right, Format = "N0"}),
                    }
                } as IItemToDisplay);
        }

        /// <summary>Excel</summary>
        protected override IEnumerable<IEnumerable<IExCell>> LoadExcelItemsToDisplay(FieldFilter filter)
        {
            return Facade.Find(filter)
                .Select(i => new List<IExCell>
                {
                    i.ExCell(e => e.PropertyName),
                    i.ExCell(e => e.ColumnName),
                    i.ExCell(e => e.DisplayName),
                    i.ExCell(e => e.Type),
                    i.ExCell(e => e.Length),
                    i.ExCell(e => e.IsStatic),
                    i.ExCell(e => e.IsRequired),
                    i.ExCell(e => e.IsUnique),
                    i.ExCell(e => e.ID),
                });
        }

        /// <summary>Filter defaults</summary>
        protected override FieldFilter ClearFilter()
        {
            var filter = base.ClearFilter();

            // TODO set the MVC defaults e.g.: filter.OrderBy["ID"] = false;
            return filter;
        }

        #region OnBefore

        protected override void OnBeforeNew()
        {
            base.OnBeforeNew();
            Model.DataTypes = Facade.GetDataTypes();
        }

        protected override void OnBeforeEdit(int id)
        {
            base.OnBeforeEdit(id);
            Model.DataTypes = Facade.GetDataTypes();
        }

        #endregion OnBefore
    }
}